package com.situ.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.domain.entity.TProductCategory;
import com.situ.domain.entity.TProductCategoryExample;
import com.situ.domain.vo.CategoryVO;
import com.situ.mapper.TProductCategoryMapper;
import com.situ.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<TProductCategoryMapper,TProductCategory> implements CategoryService {

    private final TProductCategoryMapper categoryMapper;

    @Override
    public CategoryVO showCategory() {
        TProductCategoryExample example = new TProductCategoryExample();
        TProductCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(0L);
        List<TProductCategory> firstCategoryList =
                categoryMapper.selectByExample(example);
        List<List<TProductCategory>> secondCategoryList = new ArrayList<>();
        for (TProductCategory tProductCategory : firstCategoryList) {
            TProductCategoryExample secondCategoryExample
                    = new TProductCategoryExample();
            TProductCategoryExample.Criteria secondCategoryCriteria
                    = secondCategoryExample.createCriteria();
            secondCategoryCriteria.andParentIdEqualTo(Long.valueOf(tProductCategory.getId()));
            List<TProductCategory> tProductCategories = categoryMapper.selectByExample(secondCategoryExample);
            secondCategoryList.add(tProductCategories);
        }
        return CategoryVO
                .builder()
                .firstCategoryList(firstCategoryList)
                .secondCategoryList(secondCategoryList)
                .build();
    }
}
